﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using ElectronicJournal.DB.Item;

namespace ElectronicJournal.DB
{
    public class DBTheme
    {
        SqlDb db;

        public DBTheme()
        {
            db = new SqlDb();
        }

        public string QUERY { get; set; }

        private ItemTheme FetchRowTheme(DataRow dr)
        {
            return new ItemTheme()
            {
                idTheme = Convert.ToInt32(dr["idTheme"]),
                backgroundcolor = dr["backgroundcolor"].ToString(),
                backgroundimage = dr["backgroundimage"].ToString(),
                color = dr["color"].ToString(),
                logoheader = dr["logoheader"].ToString(),
                bgimgtopleft = dr["bgimgtopleft"].ToString(),
                bgimgtopright = dr["bgimgtopright"].ToString(),
                bgimgright = dr["bgimgright"].ToString(),
                bgimgleft = dr["bgimgleft"].ToString(),
                bgimgbottomright = dr["bgimgbottomright"].ToString(),
                bgimgbottomleft = dr["bgimgbottomleft"].ToString(),
                bgimgbottom = dr["bgimgbottom"].ToString(),
                bgcolormenu = dr["bgcolormenu"].ToString(),
                bgcolormenuitem = dr["bgcolormenuitem"].ToString(),
                bordercolormenuitem = dr["bordercolormenuitem"].ToString(),
                colormenuitem = dr["colormenuitem"].ToString(),
                bgcolorfooter = dr["bgcolorfooter"].ToString()

            };
        }

        public ItemTheme GetTheme()
        {
            DataTable dt = db.Read(String.Format("SELECT * FROM Themes"));
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                return FetchRowTheme(dr);
            }
            return null;

        }
        
        public bool Update(ItemTheme Theme)
        {

            QUERY = String.Format("UPDATE Themes SET backgroundcolor='{0}',backgroundimage='{1}',color='{2}',logoheader='{3}',bgimgtopleft='{4}',bgimgtopright='{5}',bgimgright='{6}',bgimgleft='{7}',bgimgbottomright='{8}',bgimgbottomleft='{9}',bgimgbottom='{10}',bgcolormenu='{11}',bgcolormenuitem='{12}',bordercolormenuitem='{13}',colormenuitem='{14}',bgcolorfooter='{15}' Where idtheme='1'",
                Theme.backgroundcolor, Theme.backgroundimage, Theme.color, Theme.logoheader, Theme.bgimgtopleft, Theme.bgimgtopright, Theme.bgimgright, Theme.bgimgleft, Theme.bgimgbottomright, Theme.bgimgbottomleft, Theme.bgimgbottom, Theme.bgcolormenu, Theme.bgcolormenuitem, Theme.bordercolormenuitem, Theme.colormenuitem, Theme.bgcolorfooter);
            db.Query(QUERY);

            return true;

        }

    }
}